#include "PreCompiled.h"
#include "Camera.h"
using namespace Gui;

CameraRoot::CameraRoot(void)
:mIsButtonDown(false),mTimeManager(NULL),mMove(2.15f),mRotate(0.15f),mXRotate(0.0f),mYRotate(0.0f)
,wButtonDown(false),sButtonDown(false),aButtonDown(false),dButtonDown(false)
{
	mTimeManager = GetCKContext(0)->GetTimeManager();
	mInputManager = (CKInputManager*)GetCKContext(0)->GetManagerByGuid(INPUT_MANAGER_GUID);
}

CameraRoot::~CameraRoot(void)
{
}

void CameraRoot::update()
{
   rotateCamera();
 	moveCamera();
}

void CameraRoot::rotateCamera()
{ 
   return;
}
void CameraRoot::moveCamera()
{
  if(!mTimeManager)
	  return;

    if(wButtonDown)
	{
		if(mDirection.z>=5.0)
			mDirection.z = 5.0f;
		else
			mDirection *= mMove;
	}
	else if(sButtonDown)
	{
		if(mDirection.z<=-5.0)
			mDirection.z = -5.0f;
		else
			mDirection *= mMove;
	}
	else
	{     
       if(mDirection.z >= 0.1f || mDirection.z <= -0.1f)
			mDirection /=mMove;
	   else
		   mDirection.z = 0.0f;
	}

	if(aButtonDown)
	{
		if(mDirection.x <= -5.0f)
			mDirection.x = -5.0f;
		else
			mDirection.x *= mMove;
	}else if(dButtonDown)
	{
        if(mDirection.x >= 5.0f)
			mDirection.x = 5.0f;
		else
			mDirection.x *= mMove;
	}else
	{
         if(mDirection.x >= 0.1f || mDirection.x <= -0.1f)
			 mDirection.x /= mMove;
		 else
			 mDirection.x = 0.0f;
	}


	float elstime = mTimeManager->GetLastDeltaTime()/1000.0f;
    _camera->Translate(&(mDirection* elstime),_camera);
}

//////////////////////////////////////////////////////////////////////////

FreeCamera::FreeCamera(const char* CameraName)
{
	_camera = CKCamera::Cast(GetCKContext(0)->GetObjectByNameAndParentClass((char*)CameraName,CKCID_CAMERA,NULL));
}

void FreeCamera::mouseDown(QMouseEvent* e)
{
	offset = e->pos();
    mIsButtonDown = true;


	_camera->GetOrientation(&ori_dir,&ori_up,&ori_right);
}
#include "Application_App.h"
void FreeCamera::mouseMove(QMouseEvent* e)
{
    int _x = e->x() - offset.x();
	int _y = e->y() - offset.y();



	mXRotate += (_x*mRotate*0.03f);
	mYRotate += (_y*mRotate*0.03f);

	XString g;g += mXRotate;
	std::string str(g.CStr());
	str += "\n";
	std::wstring wstr = GetApplication().s2ws(str);
	OutputDebugStringW(wstr.c_str());

	offset.setX(e->x());
	offset.setY(e->y());
}

 void FreeCamera::rotateCameraByAxis(const VxVector& aix,float ang,CKCamera* cam)
{

	VxVector up,right;


   VxMatrix rotateMat;
   Vx3DMatrixFromRotation(rotateMat,aix,ang);

   ori_dir.Rotate(rotateMat);
   up = CrossProduct(ori_dir,aix);
   right = CrossProduct(ori_dir,up);
   right *= -1.0f;

   _camera->SetOrientation(&ori_dir,&up);

}

 void FreeCamera::rotateCamera()
 {
	 float elstime = mTimeManager->GetLastDeltaTime()/1000.0f;
	 float x = (0.0f-mXRotate)*elstime*5.0f;
	 _camera->Rotate(&(VxVector::axisY()),x);
	 mXRotate += x*2.5f;
 	 float y = (0.0f - mYRotate)*elstime*5.0f;
 	 _camera->Rotate(&(VxVector::axisX()),y,_camera);
 	 mYRotate += y*2.5f;

 }
void FreeCamera::mouseUp(QMouseEvent* e)
{
//     mXRotate = 0.0f;
// 	mYRotate = 0.0f;
}

void FreeCamera::keydown(QKeyEvent* e)
{

	if(e->key() == Qt::Key_W)
	{
		mDirection.z = 2.0f;
		if(sButtonDown)
			sButtonDown = false;
		wButtonDown = true;   
	}
	if(e->key() == Qt::Key_S)
	{
		mDirection.z = -2.0f;
		if(wButtonDown)
			wButtonDown = false;
		sButtonDown = true;
	}
	if(e->key() == Qt::Key_A)
	{
		mDirection.x = -2.0f;
		if(dButtonDown)
			dButtonDown = false;
       aButtonDown = true;
	}
	if(e->key() == Qt::Key_F)
	{
		mDirection.x = 2.0f;
		if(aButtonDown)
			aButtonDown = false;
       dButtonDown = true;
	}
}
void FreeCamera::keyup(QKeyEvent* e)
{
	
	if(e->key() == Qt::Key_W)
	{
		wButtonDown = false;
	}
	if(e->key() == Qt::Key_S)
	{
		sButtonDown = false;
	}
	if(e->key() == Qt::Key_A)
	{
        aButtonDown = false;
	}
	if(e->key() == Qt::Key_F)
	{
       dButtonDown = false;
	}
}
void FreeCamera::keydown(CKDWORD key)
{
	switch (key)
	{
	case CKKEY_W:
		{
			mDirection.z += mMove;
			XString g;g += mDirection.z;
			std::string str(g.CStr());
			str += "\n";
			std::wstring wstr = GetApplication().s2ws(str);
			OutputDebugStringW(wstr.c_str());
		}

		break;;

	default:
		break;
	}

}

void FreeCamera::keyup(CKDWORD key)
{
	switch (key)
	{
	case CKKEY_W:
		mDirection.z -= mMove;
		break;;

	default:
		break;
	}
}

void FreeCamera::wkeydwon()
{
	if(mDirection.z>=2.0)
		mDirection.z = 2.0f;
	else
 	   mDirection.z += mMove;
}
void FreeCamera::wkeyup()
{
    if(mDirection.z<=0.0f)
		mDirection.z = 0.0f;
	else
		mDirection.z -= mMove;
}

//////////////////////////////////////////////////////////////////////////

OribitCamera::OribitCamera(const char* cn)
{

}

void OribitCamera::mouseDown(QMouseEvent* e)
{

}

void OribitCamera::mouseMove(QMouseEvent* e)
{

}
void OribitCamera::mouseUp(QMouseEvent* e)
{

}
void OribitCamera::keydown(QKeyEvent* e)
{

}
void OribitCamera::keyup(QKeyEvent* e)
{

}
CameraManager* CameraManager::_instance = 0;
CameraManager::CameraManager()
{

}
CameraManager::~CameraManager()
{

}

CameraManager* CameraManager::Instance()
{
	if(_instance == 0)
		_instance = new CameraManager();

	return _instance;
}

void CameraManager::addCamera(CameraRoot* ca)
{
	_cameraList.push_back(ca);
}

void CameraManager::setActiveCamera(CameraRoot* ca)
{
	activeCamera = ca;
}
void CameraManager::mousebuttonDown(QMouseEvent* e)
{
	if(activeCamera)
		activeCamera->mouseDown(e);
// 	_cameraItor = _cameraList.begin();
// 	for(; _cameraItor != _cameraList.end(); _cameraItor++)
// 	{
// 		if( (*_cameraItor) == ca)
// 			(*_cameraItor)->mouseDown();
// 	}
}

void CameraManager::mousebuttonMove(QMouseEvent* e)
{
	if(activeCamera)
		activeCamera->mouseMove(e);
// 	_cameraItor = _cameraList.begin();
// 	for(; _cameraItor != _cameraList.end(); _cameraItor++)
// 	{
// 		if( (*_cameraItor) == ca)
// 		     (*_cameraItor)->mouseMove();
// 	}
}

void CameraManager::mousebuttonUp(QMouseEvent* e)
{
	if(activeCamera)
		activeCamera->mouseUp(e);
// 	_cameraItor = _cameraList.begin();
// 	for(; _cameraItor != _cameraList.end(); _cameraItor++)
// 	{
// 		if( (*_cameraItor) == ca)
// 			(*_cameraItor)->mouseUp();
// 	}
}

void CameraManager::keydown(QKeyEvent* e)
{
	if(activeCamera)
		activeCamera->keydown(e);
}
void CameraManager::keyup(QKeyEvent* e)
{
	if(activeCamera)
		activeCamera->keyup(e);
}
void CameraManager::updateCamera()
{
	if(activeCamera)
		activeCamera->update();
}

void CameraManager::keydown(CKDWORD key)
{
   if(activeCamera)
	   activeCamera->keydown(key);
}
void CameraManager::keyup(CKDWORD key)
{
   if(activeCamera)
	   activeCamera->keyup(key);
}

void CameraManager::wkeydown()
{
   if(activeCamera)
	   activeCamera->wkeydwon();
}
void CameraManager::wkeyup()
{
	if(activeCamera)
		activeCamera->wkeyup();
}


